<template>
  <div class="main">
    <!-- <div class="title">Weslots</div> -->
    <div class="title">
      <img src="@/assets/img/bannerIcon.png" alt="" />
    </div>
    <div class="cont">
      <div class="login-type-box">
        <div
          class="lable"
          :style="selectType === 'email' ? 'color: #ffff' : ''"
          @click="selectType = 'email'"
        >
          {{ $t("Email") }}
        </div>
        <!-- Email -->
        <!-- <div
          class="lable"
          :style="selectType === 'mobile' ? 'color: #ffff' : ''"
          @click="selectType = 'mobile'"
        >
          Phone
        </div> -->
      </div>
      <div class="phonbox" v-if="selectType === 'email'">
        <div class="p-l email-p-l">
          <van-icon name="envelop-o" class="email-icon" />
        </div>
        <!-- email -->
        <input
          :placeholder="$t('Email')"
          v-model="email"
          @input="inputValueChange"
        />
      </div>
      <div class="phonbox" v-else>
        <div class="p-l" @click="goPhoneNumber">
          <img class="phone-icon" src="@/assets/img/phoneNumber.png" />
          <div class="phone-qu">
            <van-loading size="24" v-if="!areaCode" />{{ areaCode }}
          </div>
          <img class="select-icon" src="@/assets/img/select-right.png" />
        </div>
        <!-- Phone number -->
        <input
          type="text"
          :placeholder="$t('NomorTelepon')"
          @input="inputValueChange"
          :disabled="!areaCode"
          v-model="mobile"
          @keyup="mobile = mobile.replace(/[^\d]/g, '')"
          @afterpaste="mobile = mobile.replace(/[^\d]/g, '')"
        />
      </div>
      <!-- Password -->
      <div class="lable">{{ $t("Password") }}</div>
      <div class="codebox">
        <div class="p-l">
          <img class="phone-icon" src="@/assets/img/lock.png" />
        </div>
        <input
          type="password"
          @input="inputValueChange"
          :class="{ active: password.trim() }"
          v-model="password"
          @keyup="password = password.trim()"
          :placeholder="$t('Password')"
        />
      </div>

      <div :class="['loginbtn', { disabled: disabled }]">
        <div class="lbcenter" @click="login">
          <template v-if="loginAjax">
            <van-loading color="#fff" />
          </template>
          <!-- Log in -->
          <template v-if="!loginAjax">{{ $t("LogIn") }}</template>
        </div>
      </div>
      <div class="btnlist">
        <div class="bl-l" @click="$router.push('/resetPassword')">
          {{ $t("ForgotPassword") }}
          <!-- Lupa password -->
        </div>
        <div class="bl-r" @click="gotoRegister">
          {{ $t("ToRegister") }}
          <!-- Untuk Mendaftar -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  httpIpLocation,
  httpGetPhoneAreaCode,
  httpMobileLogin,
  httpInfo,
} from "@/http/api";
export default {
  name: "login",
  data() {
    return {
      selectType: "email",
      email: "",
      password: "",
      phoneAreaList: [],
      areaCode: localStorage.getItem("areaCode") || "",
      mobile: "",
      loginAjax: false,
      disabled: true,
    };
  },
  created() {
    if (!this.areaCode) this.getHttpGetPhoneAreaCode();
    this.selectType = this.$route.query.selectType
      ? this.$route.query.selectType
      : this.$t("email");
    if (localStorage.getItem("TOKEN")) this.$router.push("/");
  },
  methods: {
    gotoRegister() {
      // 获取浏览器 URL 中的 invited_code 参数
      const urlParams = new URLSearchParams(window.location.search);
      const invitedCode = urlParams.get("invite_code");
      //console.log("invitedCode:", invitedCode);
      // 构建 register 页面的路径，并将 invited_code 作为参数传递
      const registerPath = {
        path: "/register",
        query: { invite_code: invitedCode },
      };

      // 使用 $router.push 导航到 register 页面
      this.$router.push(registerPath);
    },
    inputValueChange() {
      if (this.password.trim() && (this.mobile.trim() || this.email))
        this.disabled = false;
      else this.disabled = true;
    },
    // 获取手机区号,所属国家
    async getHttpGetPhoneAreaCode() {
      let res = await httpGetPhoneAreaCode();
      if (res.code == 0) {
        this.phoneAreaList = res.data || [];
      }
      this.areaCode = "+62";
      // this.getHttpIpLocation();
    },
    // 获取当前ip信息
    async getHttpIpLocation() {
      let res = await httpIpLocation();
      if (res.code == 0) {
        let country = res.data?.country || "";
        this.phoneAreaList.forEach((v) => {
          if (v.country == country) this.areaCode = v.area_code;
        });
        if (!this.areaCode) this.areaCode = "+62";
      }
    },

    // login
    async login() {
      if (this.loginAjax) return;
      if (!this.mobile.trim() && !this.email) return;
      // this.$Toast.fail("Incorrect account number");
      if (!this.password.trim()) return;
      // this.$Toast.fail("Incorrect password");

      let params = {
        mobile_area_code: this.areaCode,
        account_name: this.selectType === "mobile" ? this.mobile : this.email,
        password: this.password,
        type: this.selectType,
      };

      let res = await httpMobileLogin(params);
      if (res.code == 0) {
        this.$Toast.success(this.$t("Success"));
        if (res.code == 0) {
          localStorage.setItem("TOKEN", res.data.token);
          // 获取用户信息
          let res2 = await httpInfo();
          if (res2.code == 0) {
            localStorage.setItem("email", this.email.trim());
            localStorage.setItem("mobile", this.mobile);
            localStorage.setItem("areaCode", this.areaCode);
            localStorage.setItem("account_name", res2.data.account_name);
            localStorage.setItem("invite_code", res2.data.invite_code);
            localStorage.setItem("uid", res2.data.uid);
            localStorage.setItem("avatar", res2.data.avatar);
            localStorage.setItem("is_agent", res2.data.is_agent);
            localStorage.setItem(
              "accept_language",
              res2.data["accept-language"] || "en"
            );
            this.$router.push("/");
          }
        }
      } else {
        this.$Toast.fail(res.message);
      }
      this.loginAjax = false;
    },

    goPhoneNumber() {
      if (!this.areaCode) return;

      this.$router.push({
        name: `phoneNumber`,
        query: {
          selectType: this.selectType,
          type: 1,
        },
      });
    },
  },
};
</script>

<style lang="less" scoped>
* {
  color: #ffffff;
}

.main {
  min-height: 100vh;
  width: 100%;

  .login-type-box {
    display: flex;
  }

  .title {
    text-align: center;
    padding-top: 280px;
    font-size: 80px;
    font-weight: 700;
    // color: rgba(112, 126, 162, 1);
    color: #ffffff;
    margin-bottom: 100px;

    img {
      display: inline-block;
      width: 200px;
      height: 200px;
    }
  }

  .cont {
    width: 100%;
    padding: 0 65px;

    .lable {
      font-size: 24px;
      color: rgba(171, 182, 212, 1);
      margin-bottom: 16px;
      margin-right: 10px;
    }

    .phonbox {
      width: 100%;
      display: flex;
      align-content: center;
      border-bottom: 1px solid rgba(221, 228, 243, 1);
      margin-bottom: 58px;
      padding-bottom: 24px;

      .email-p-l {
        width: 60px !important;
      }

      .p-l {
        width: 160px;
        height: 38px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-right: 1px solid rgba(221, 228, 243, 1);
        flex-shrink: 0;

        .email-icon {
          // color: rgba(171, 182, 212, 1);
          color: #ffffff;
        }

        .phone-icon {
          width: 38px;
          height: 38px;
        }

        .select-icon {
          width: 40px;
          height: 40px;
        }

        .phone-qu {
          font-size: 24px;
          // color: rgba(112, 126, 162, 1);
          color: #ffffff;
        }
      }
    }

    .codebox {
      width: 100%;
      display: flex;
      align-content: center;
      border-bottom: 1px solid rgba(221, 228, 243, 1);
      margin-bottom: 105px;
      padding-bottom: 24px;

      .p-l {
        width: 60px;
        height: 38px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-right: 1px solid rgba(221, 228, 243, 1);
        flex-shrink: 0;

        .phone-icon {
          width: 38px;
          height: 38px;
        }
      }
    }

    input {
      padding-left: 24px;
      border: none;
      flex: 1;
      outline: none;
      font-size: 28px;
      // font-weight: 700;
      // color: rgba(132, 144, 177, 1);
      color: #ffffff;
      background: transparent;

      &::-webkit-input-placeholder {
        // color: rgba(171, 182, 212, 1);
        color: #ffffff;
        font-size: 24px;
        font-weight: 400;
      }

      &.active {
        font-size: 40px;
        line-height: 38px;
        letter-spacing: 0px;
        height: 38px;
        max-width: 520px;
      }
    }

    .loginbtn {
      // text-shadow: 1px 3px 1px 0px rgba(212, 161, 110, 0.88);
      font-size: 30px;
      font-weight: 700;
      letter-spacing: 2.4px;
      // color: rgba(255, 255, 255, 1);
      display: flex;
      align-items: center;
      justify-content: center;
      width: 618px;
      height: 112px;
      opacity: 1;
      /**background: linear-gradient(
        178.44deg,
        rgba(255, 255, 255, 1) 0%,
        rgba(255, 226, 196, 1) 100%
      );
      box-shadow: 0px 7px 9px 0px rgba(201, 173, 144, 0.33);**/
      background: url("~@/assets/img/19.png") center/100% 100% no-repeat;
      border-radius: 24px;
      margin-bottom: 52px;

      .lbcenter {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 594px;
        height: 88px;
        opacity: 1;
        color: rgba(255, 255, 255, 1);
        /**background: linear-gradient(
          178.44deg,
          rgba(238, 220, 201, 1) 0%,
          rgba(220, 188, 155, 1) 100%
        );
        box-shadow: 1px 7px 8px 0px rgba(201, 173, 144, 0.51),
          inset 0px 3px 9px 0px rgba(197, 146, 92, 1);
        border-radius: 24px;**/
        user-select: none;
      }

      &.disabled {
        box-shadow: 0px 7px 9px 0px rgba(201, 173, 144, 0);
        background: #ccc;

        .lbcenter {
          box-shadow: 1px 7px 8px 0px rgba(201, 173, 144, 0),
            inset 0px 3px 9px 0px rgba(197, 146, 92, 0);
          background: #ccc;
        }
      }
    }

    .btnlist {
      display: flex;
      width: 100%;
      justify-content: space-between;
      align-items: center;
      font-size: 28px;
      user-select: none;
    }
  }
}
</style>
